# -*- coding: utf-8 -*-
"""
Macana XML-RPC Dispatcher for Taíno
Copyright (C) 2009, Joshua "jag" Ginsberg <jag@flowtheory.net

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from django import http
from macana.dispatcher import MacanaXMLRPCDispatcher
from macana.api import IMacanaAuthentication, IMacanaXMLRPCMethod

dispatcher = MacanaXMLRPCDispatcher(IMacanaAuthentication, IMacanaXMLRPCMethod)

def xmlrpc_listener(request):
    if request.method == "POST":
        try:
            xmlrpc_response = dispatcher._marshaled_dispatch(request)
        except:
            return http.HttpResponseServerError()
        else:
            return http.HttpResponse(content=xmlrpc_response,
                                     mimetype='text/xml')
    else:
        return http.HttpResponse(
            content=u'This is an XMLRPC listener. See http://xmlrpc.com/spec to learn how to interact with it.', 
            mimetype='text/plain')
